﻿using System;
using System.Windows.Controls;


namespace Akabana.Sample.Greeting.Silverlight.ClientApp
{

    public class PersonType
    {
        public string Label { get; set; }

        public Type Type { get; set; }

    }

    public partial class MainPage : UserControl
    {
        public GreetingService greetingService;

        public MainPage()
        {
            InitializeComponent();
            PersonType.ItemsSource = CreatePersonTypes();
            PersonType.SelectedIndex = 0;
        }

        private static PersonType[] CreatePersonTypes()
        {
            return new PersonType[] {
                new PersonType() { Label = "日本語", Type=typeof(Japanese) },
                new PersonType() { Label = "English", Type=typeof(English) },
                new PersonType() { Label = "その他" },
            };
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var nowDate = DateTime.Now;
            var personType = (PersonType.SelectedItem as PersonType).Type;
            Person person;
            if (personType == null)
            {
                person = new Person();
            }
            else
            {
                person = Activator.CreateInstance(personType) as Person;
            }
            person.startDateTime = nowDate;
            person.endDateTime = nowDate;
            greetingService
                .GetGreeting(
                    person,nowDate,
                    greeting =>
                    {
                        LayoutResult.DataContext = greeting;
                    },
                    fault =>
                    {
                        DebugText.Text = string.Format("エラー:{0}", fault.FaultDetail);
                    }
                );

        }
    }

}
